package io.zhifei.modules.sys.dataStatistical.util;
/*
 * @Author wangyuanyue
 * @Date 2020/12/25 15:04
 * @Content 周工具类
 */

import io.zhifei.modules.sys.dataStatistical.entity.DayOfWeekInfo;
import io.zhifei.modules.sys.dataStatistical.entity.WeekOfMonthInfo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class WeekUtil {

    /**
     * 自然每天获得方式
     * @param dayOfWeek
     * @return
     */
    public static Map<String,String> everyDayOfWeek(String dayOfWeek) throws ParseException {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式
        Calendar cal = Calendar.getInstance();
        Date time=sdf.parse(dayOfWeek);
        cal.setTime(time);
        //判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
        if(1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        cal.setFirstDayOfWeek(Calendar.MONDAY);//设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        int day = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-day);//根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        Map<String,String> everyDayMap = new LinkedHashMap<>();
        cal.add(Calendar.DATE, -1);
        for(int i = 0 ; i < 7 ; i++){
            cal.add(Calendar.DATE, 1);
            everyDayMap.put("num_"+i,sdf.format(cal.getTime()));
        }
        return everyDayMap;
    }


    /**
     * 自然周获得方式
     * @param dayOfWeek
     * @return
     */
    public static List<DayOfWeekInfo> dayOfWeek(String dayOfWeek) throws ParseException {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式
        Calendar cal = Calendar.getInstance();
        Date time=sdf.parse(dayOfWeek);
        cal.setTime(time);
        //判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
        if(1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        cal.setFirstDayOfWeek(Calendar.MONDAY);//设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        int day = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-day);//根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        List<DayOfWeekInfo> dayOfWeekInfos = new ArrayList<>();
        DayOfWeekInfo oneToThree = new DayOfWeekInfo();
        oneToThree.setDayOfWeekStartTime(sdf.format(cal.getTime()));    //所在周星期一的日期
        cal.add(Calendar.DATE, 2);
        oneToThree.setDayOfWeekEndTime(sdf.format(cal.getTime()));      //所在周星期三的日期
        dayOfWeekInfos.add(oneToThree);
        DayOfWeekInfo fourToFive = new DayOfWeekInfo();
        cal.add(Calendar.DATE, 1);
        fourToFive.setDayOfWeekStartTime(sdf.format(cal.getTime()));    //所在周星期四的日期
        cal.add(Calendar.DATE, 1);
        fourToFive.setDayOfWeekEndTime(sdf.format(cal.getTime()));  //所在周星期五的日期
        dayOfWeekInfos.add(fourToFive);
       /* DayOfWeekInfo sixToSeven = new DayOfWeekInfo();
        cal.add(Calendar.DATE, 1);
        sixToSeven.setDayOfWeekStartTime(sdf.format(cal.getTime()));    //所在周星期六的日期
        cal.add(Calendar.DATE, 1);
        sixToSeven.setDayOfWeekEndTime(sdf.format(cal.getTime()));  //所在周星期日的日期
        dayOfWeekInfos.add(sixToSeven);*/
        return dayOfWeekInfos;
    }




    /**
     * 周变月工具方法
     * @param yearMonth
     * @return
     */
    public static List<WeekOfMonthInfo> weekOfMonth(String yearMonth) throws ParseException {
        //获得每月周
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Date date1 = sdf.parse(yearMonth);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date1);
        int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        System.out.println("days:" + days);
        int count = 0;
        List<WeekOfMonthInfo> result = new ArrayList<>();
        Map<String,String> weekOfMonthChinese = new HashMap<>();
        weekOfMonthChinese.put("1","一");
        weekOfMonthChinese.put("2","二");
        weekOfMonthChinese.put("3","三");
        weekOfMonthChinese.put("4","四");
        weekOfMonthChinese.put("5","五");
        weekOfMonthChinese.put("6","六");
        for (int i = 1; i <= days; i++) {
            SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
            Date date2 = dateFormat1.parse(yearMonth + "-" + i);
            calendar.clear();
            calendar.setTime(date2);
            int k = new Integer(calendar.get(Calendar.DAY_OF_WEEK));
            if (k == 1) {// 若当天是周日
                count++;
                String str1 = "";
                String str2 = "";
                if (i - 6 <= 1) {
                    str1 = yearMonth + "-" + 1;
                } else {
                    str1 = yearMonth + "-" + (i - 6);
                }
                str2 = yearMonth + "-" + i;
                WeekOfMonthInfo weekOfMonthInfo = new WeekOfMonthInfo();
                weekOfMonthInfo.setWeekOfMonth("第" + weekOfMonthChinese.get(count+"") + "周"+str1.substring(str1.indexOf("-")+1)+"至"+str2.substring(str2.indexOf("-")+1));
                weekOfMonthInfo.setWeekStartDate(str1);
                weekOfMonthInfo.setWeekEndDate(str2);
                result.add(weekOfMonthInfo);
            }
            if (k != 1 && i == days) {// 若是本月最好一天，且不是周日
                count++;
                String str1 = "";
                String str2 = "";
                str1 =  yearMonth + "-" + (i - k + 2);
                str2 =  yearMonth + "-" + i;
                WeekOfMonthInfo weekOfMonthInfo = new WeekOfMonthInfo();
                weekOfMonthInfo.setWeekOfMonth("第" + weekOfMonthChinese.get(count+"")  + "周"+str1.substring(str1.indexOf("-")+1)+"至"+str2.substring(str2.indexOf("-")+1));
                weekOfMonthInfo.setWeekStartDate(str1);
                weekOfMonthInfo.setWeekEndDate(str2);
                result.add(weekOfMonthInfo);
            }
        }
        return result;
    }
}
